package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.SetmealDish;
import com.sky.entity.ShoppingCart;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.*;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private FlavorMapper flavorMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Override
    @Transactional
    public void saveWithFlavors(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO , dish);
        //插入新菜品的信息
        dishMapper.insert(dish);
        //菜品口味的插入
        List<DishFlavor> flavors = dishDTO.getFlavors();
        //口味不为空
        if(flavors != null && flavors.size() != 0){
           flavors.forEach(f->{
               f.setDishId(dish.getId());
           });
           flavorMapper.insertBatch(flavors);
        }
    }

    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        //开始分页
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        //查询数据
        Page<DishVO>  pageList = dishMapper.pageQuery(dishPageQueryDTO);

        return new PageResult(pageList.getTotal(),pageList.getResult());
    }

    @Override
    @Transactional
    public void deleteById(Long[] ids) {
        for (Long id : ids) {
            //根据 id查询，返回菜品对象
            Dish dish = dishMapper.getDishById(id);
            //1.商品在没有停售无法删除
            if(dish.getStatus() == 1){
                throw new DeletionNotAllowedException("菜品热卖中，无法删除");
            }
            List<Long> l = setmealMapper.getByIds(ids);
            //2.套餐中包含此商品无法删除
            if( ids != null && l.size() > 0 ){
                throw new DeletionNotAllowedException("套餐中包含此商品，无法删除");
            }
            //3.删除
            dishMapper.deleteById(id);
            //4.还需要删除用户购物车中的数据
            shoppingCartMapper.deleteByDishId(id);
            flavorMapper.deleteByid(id);
        }
    }

    @Override
    public DishVO getById(Long id) {
        Dish dish = dishMapper.getDishById(id);

        List<DishFlavor> flavors = flavorMapper.getFlavorsById(id);

        DishVO dishVO = new DishVO();

        BeanUtils.copyProperties(dish,dishVO);

        dishVO.setFlavors(flavors);

        return dishVO;
    }

    /**
     * 修改信息
     * @param dishDTO
     */
    @Override
    @Transactional
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        //修改菜表信息
        dishMapper.update(dish);
        //同时需要修改用户购物车中的数据
        List<DishFlavor> dishFlavorList = flavorMapper.getByDishId(dish.getId());
        ShoppingCart cart = ShoppingCart.builder().amount(dish.getPrice()).dishId(dish.getId()).image(dish.getImage()).name(dish.getName()).build();
        shoppingCartMapper.updateByDishId(cart);
        //需要判断套餐中是否包含此菜品，如果包含，需要修改
        List<SetmealDish> setmealDishes = setmealDishMapper.getById(dish.getId());
        if(setmealDishes != null && setmealDishes.size() > 0){
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setName(dishDTO.getName());
                setmealDishMapper.update(setmealDish);
            });
        }

    }

    /**
     * 修改起售和停售
     * @param dish
     */
    @Override
    public void update(Dish dish) {
        //需要判断套餐是否包含此商品，如果包含，不可以停售
        List<SetmealDish> setmealDishes = setmealDishMapper.getById(dish.getId());
        if(setmealDishes != null && setmealDishes.size() > 0){
            throw new DeletionNotAllowedException("套餐中包含此菜品，无法停售");
        }
        dishMapper.update(dish);
        //还需要删除用户购物车中的数据
        shoppingCartMapper.deleteByDishId(dish.getId());
    }

    @Override
    public List<DishVO> getByCategoryId(Long categoryId) {
        List<DishVO> dishVos = dishMapper.getByCategoryId(categoryId);
        return dishVos;
    }

    @Override
    public List<DishVO> getDishWithFlavor(Long categoryId) {
        List<DishVO> list = dishMapper.getByCategoryId(categoryId);

        for (DishVO dishVO : list) {
            List<DishFlavor> flavors = flavorMapper.getByDishId(dishVO.getId());
            dishVO.setFlavors(flavors);
        }

        return list;
    }


}
